import { no_del, loc, obj_str, is_empty, cline, uDb } from 'sui';

const tp_list = () => {
    return [
        {sn: "string", name: loc("l_630")},
        {sn: "num", name: loc("l_459")},
        {sn: "date", name: loc("l_631")},
    ];
};

const get_fix = (fix, data) => {
    const ret = [];
    const set = new Set();

    if (!is_empty(data)) {
        data.forEach(d => set.add(obj_str(d, "sn")));
    }

    const fix_set = {};

    if (!is_empty(fix)) {
        fix.forEach(d => {
            const sn = obj_str(d, "sn");

            if (set.has(sn)) {
                fix_set[sn] = d;
                return;
            }

            ret.push(d);
        });
    }

    if (!is_empty(data)) {
        data.forEach(d => {
            const sn = obj_str(d, "sn");

            const f = fix_set[sn];
            if (f) {
                d["ctp"] = f["ctp"];
                d["ctl"] = f["ctl"];
            }

            ret.push(d);
        });
    }

    ret.forEach((d, i) => d["index"] = i + 1);

    return ret;
};

const reload_col = (hDone, hcmd, mod, fix) => {
    uDb(hDone, `field/list/${mod}`,
        {
            "line": cline(),
        },
        r => {
            const col = get_fix(fix, r);
            hcmd(col);
        },
        e => {
            const col = get_fix(fix, null);
            hcmd(col);
        });
};

const reload = (hDone, umd, mod, fix) => reload_col(hDone, v => umd.setValue(v), mod, fix);

const update = (umd, mod, v, fix) => {
    umd.startWait();

    uDb(null, `field/md/${mod}`,
        {
            "line": cline(),
            "v": v,
        },
        r => reload(umd.endWait, umd, mod, fix),
        e => umd.setErr(loc("l_232")), true);
};

const reload_prop = (hDone, hcmd, mod, cur) => {
    uDb(hDone, `file/get/${mod}`,
        {
            "line": cline(),
            "sn": obj_str(cur, "sn"),
        },
        r => {
            const para = r["para"];
            para.forEach((d, i) => d["index"] = i + 1);
            hcmd(para);
        },
        e => hcmd(null));
};

const update_prop = (umd, mod, v, cur) => {
    umd.startWait();

    const data = v.map(d => {
        return {
            sn: obj_str(d, "sn"),
            name: obj_str(d, "name"),
        };
    });

    uDb(null, `file/md/${mod}`,
        {
            line: cline(),
            sn: obj_str(cur, "sn"),
            v: {para: data},
        },
        r => reload_prop(umd.endWait, v => umd.setValue(v), mod, cur),
        e => umd.setErr(loc("l_232")), true);
};

const action_swap = (val, umd) => {
    const fin = umd.fin;
    const ret = [...umd.value];

    let pos = 0;

    const f = ret.find((d, i) => {
        pos = i;
        return d.index === fin.cur;
    });

    if (!f) {
        return null;
    }

    if (val.tp === "up") {
        if (pos === 0) {
            return;
        }

        const a = ret[pos - 1];
        const b = ret[pos];

        ret[pos - 1] = b;
        ret[pos] = a;
    } else if (val.tp === "down") {
        if (pos === ret.length - 1) {
            return;
        }

        const a = ret[pos];
        const b = ret[pos + 1];

        ret[pos] = b;
        ret[pos + 1] = a;
    }

    ret.forEach((d, i) => d["index"] = i + 1);

    return ret;
};

const action_edit = (val, umd) => {
    const fin = umd.fin;
    const data = umd.value;
    const ret = [];

    data.forEach(d => {
        if (d.index === fin.cur) {
            ret.push(val)
            return;
        }

        ret.push(d);
    });

    ret.forEach((d, i) => d["index"] = i + 1);

    return ret;
};

const action_add = (val, umd) => {
    const fin = umd.fin;
    const data = umd.value;
    const ret = [];

    if (is_empty(data)) {
        ret.push(val);
    } else {
        data.forEach(d => {
            ret.push(d);

            if (d.index === fin.cur) {
                ret.push(val)
            }
        });
    }

    ret.forEach((d, i) => d["index"] = i + 1);

    return ret;
};

const action_del = (val, umd) => {
    if (no_del()) {
        return null;
    }

    const index = val.index;
    const data = umd.value;
    const ret = [];

    data.forEach(d => {
        if (d.index === index) {
            return;
        }

        ret.push(d);
    });

    ret.forEach((d, i) => d["index"] = i + 1);

    return ret;
};

export {action_del, action_add, action_edit, action_swap, update_prop, reload_prop, tp_list, reload_col, update, reload};
